<?php

namespace App\Lib\Cache;

use App\Model\Video as VideoModel;
use EasySwoole\FastCache\Cache;
use \EasySwoole\Component\Di;

class Video
{

    public function setIndexVideo()
    {
        $catIds = array_keys(\Yaconf::get('category.cats'));
        $cacheType = \Yaconf::get('base.indexCacheType');
        array_unshift($catIds,0);
        $videoModel = new VideoModel();
        foreach($catIds as $catId){
            $condition = [];
            if(!empty($catId)){
                $condition['cat_id'] = $catId;
            }
            try {
                $data = $videoModel->getVideoCacheData($condition);
            }catch(\Exception $e){
                $data = [];
            }
            if(empty($data)){
                continue;
            }
            foreach($data as &$list){
                $list['create_time'] = date('Ymd H:i:s',$list['create_time']);
                $list['video_duration'] = gmstrftime('%H:%M:%S',$list['video_duration']);
            }
            switch ($cacheType) {
                case 'file':
                    $res = file_put_contents($this->getVideoCatIdFile($catId),json_encode($data));
                break;
                case 'table':
                    $res = Cache::getInstance()->set($this->getCatKey($catId),$data);
                break;
                case 'redis':
                    $res = Di::getInstance()->get('REDIS')->set($this->getCatKey($catId),$data);
                break;
                default:
                    throw new \Exception();  
                break;     
            }
            
            // if(empty($flag)){
            //     echo 'cat_id'.$catId.'put data error'.PHP_EOL;
            // }else{
            //     echo 'cat_id'.$catId.'put data success'.PHP_EOL;
            // }
            // Cache::getInstance()->set('index_video_data_cat_id_'.$catId,$data);
            // Di::getInstance()->get('REDIS')->set('index_video_data_cat_id_'.$catId,$data);
            
        }
        
    }

    public function getCache($catId)
    {
        $cacheType = \Yaconf::get('base.indexCacheType');
        switch ($cacheType) {
            case 'file':
                $videoFile = $this->getVideoCatIdFile($catId);
                $videoData = is_file($videoFile) ? file_get_contents($videoFile) : [];
                $videoData = !empty($videoData) ? json_decode($videoData,true) : [];
            break;
            case 'table':
                $videoData = Cache::getInstance()->get($this->getCatKey($catId));
                $videoData = !empty($videoData) ? $videoData : [];
            break;
            case 'redis':
                $videoData = Di::getInstance()->get('REDIS')->get($this->getCatKey($catId));
                $videoData = !empty($videoData) ? json_decode($videoData,true) : [];
            break;
            default:
                throw new \Exception('参数错误');  
            break;     
        }
        return $videoData;
    }

    public function getVideoCatIdFile($catId)
    {
        return EASYSWOOLE_ROOT.'/webroot/video/json/'.$catId.'.json';
    }

    public function getCatKey($catId)
    {
        return 'index_video_data_cat_id_'.$catId;
    }

}